//index.js
//获取应用实例

import {ajax} from '../../utils/httpRequest.js'

import {URI_INDEXDATA} from '../../api.js';
const app = getApp()
Page({
  data: {
    reqPage: 1,
    limit: 20,
    inputVal: '',
    IndexConfig: {},//先把4 大标题和文字存起来；因为上拉加载的时候没有这个数据
    icon: '',
    goods_list: [],
    showGoTop:false,
    text:'正在加载····',
    show_loading:false,
    sw: true //控制滚动到底部是否加载
  },
  onLoad: function () {
    var that = this;
    this.getPageData();
  },

  getPageData: function () {
    var that = this;
    ajax.get(URI_INDEXDATA, {
      page: that.data.reqPage,
      accessToken: wx.getStorageSync('accessToken')
    }, {}, (res) => {
      //console.log(res.data.data);
      if (!that.data.icon) {
        wx.setStorageSync('banner', res.data.data.IndexConfig.banner);
        that.setData({
          IndexConfig: res.data.data.IndexConfig,
          icon: res.data.data.ico,
          showGoTop:false,
          text:'正在加载····'
        })
      }
      that.setData({
        goods_list:res.data.data.indexList
      });
      wx.hideNavigationBarLoading();
      wx.stopPullDownRefresh();
    })
  },
  getMorePageData() {
    var that = this;
    ajax.get(URI_INDEXDATA, {
      page: that.data.reqPage,
      accessToken: wx.getStorageSync('accessToken')
    }, {}, (res) => {
      that.setData({
        goods_list: that.data.goods_list.concat(res.data.data.indexList),
        show_loading:false,
        sw: true
      });
      if (res.data.data.indexList < that.data.limit) {
        page.setData({
          sw: false,
          text:'---暂无数据---'
        })
        setTimeout(()=>{
          page.setData({
            show_loading:false
          });
        },1000)
      }
      console.log(that.data.goods_list);
    })
  },
  //监听input val
  _watchInput(e) {
    //console.log(e.detail.value);
    this.setData({
      inputVal: e.detail.value
    })
  },
  _inputConfirm(e) {
    if (e.detail.value) {
      wx.navigateTo({
        url: `/pages/searchResult/searchResult?val=${e.detail.value}`
      })
    } else {
      wx.showModal({
        title: '温馨提示',
        content: '输入不能为空',
        confirmColor: '#ff474f'
      })
    }

  },
  _toSearchResult() {
    if (this.data.inputVal) {
      wx.navigateTo({
        url: `/pages/searchResult/searchResult?val=${this.data.inputVal}`
      })
    } else {
      wx.showModal({
        title: '温馨提示',
        content: '输入不能为空',
        confirmColor: '#ff474f'
      })
    }

  },
  //4大标题点击事件
  titleClick: function (e) {
    var title = e.currentTarget.dataset.title;
    var key = e.currentTarget.dataset.key;
    wx.navigateTo({
      url: `/pages/titleCategory/titleCategory?key=${key}&title=${title}`
    })

  },
  // toDetails:function (e) {
  //   var item = e.currentTarget.dataset.item;
  //   //console.log(e.currentTarget.dataset.item);
  //   var data= JSON.stringify(e.currentTarget.dataset.item);
  //   wx.navigateTo({
  //     url: `/pages/details/details?goodId=${item.goodsId}&title=${item.title}&md5=${item.md5}&platform=${item.platform}&item=${data}`
  //   })
  //
  // },
  goTop: function (e) {
    if (wx.pageScrollTo) {
      wx.pageScrollTo({
        scrollTop: 0
      })
    } else {
      wx.showModal({
        title: '提示',
        content: '当前微信版本过低，无法使用该功能，请升级到最新微信版本后重试。',
        confirmColor: '#ff474f'
      })
    }
  },

  onPageScroll(e){
    var scroll = e.scrollTop;
    if(scroll>500){
      this.setData({showGoTop:true})
    }else{
      this.setData({showGoTop:false})
    }
  },
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    wx.showNavigationBarLoading();
    this.getPageData();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function (e) {
    console.log(e);
    var that = this;
    this.setData({
      show_loading:true
    });
    if (this.data.sw) {
      this.setData({
        reqPage: this.data.reqPage + 1
      })
      this.getMorePageData()
    }

  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function (res) {
    if (res.form === 'button') {
      //来自页内转发
      console.log(res.target);
    }
    return {
      title: '择天惠-一个做优惠的小程序',
      path: 'pages/index/index',
      success: function (res) {
        console.log('转发成功');
      },
      fail: function (err) {
        console.log('转发失败')
      }
    }
  }
});
